// 导入express
const express=require('express')
// 创建web服务器
const app=express()
/* 解决跨域问题！！！ */
const cros=require('cors')
app.use(cros())
app.use(express.json())
app.use(express.urlencoded())

app.get("/routes",(req,res)=>{
    if(req.query.id==1000){
        res.json({
            data:[
                {
                    path:"/page1",
                    name:'page1',
                    component:"page1"
                },
                {
                    path:"/page2",
                    name:'page2',
                    component:"page2"
                }
            ]
        })
    }else if(req.query.id==1001){
        res.json({
            data:[
                {
                    path:"/page1",
                    name:'page1',
                    component:"page1"
                },
                {
                    path:"/page2",
                    name:'page2',
                    component:"page2"
                },
                {
                    path:"/page3",
                    name:'page3',
                    component:"page3"
                },
                {
                    path:"/page4",
                    name:'page4',
                    component:"page4"
                }
            ]
        })
    }
})

app.post("/login",(req,res)=>{
    let {username,password}=req.body
    console.log(req.body);
    if(username=='xiaoyu' && password=='123'){
        res.json({
            id:1000,
            token:"dhjdfdjfdfhjjh123"
        })
    }else if(username=='admin' && password =='12345'){
        res.json({
            id:1001,
            token:"ajdosjsdjhdfhj12345"
        })
    }else{
        res.json({
            message:"登录失败"
        })
    }
})

// 启动web服务器
app.listen(8080,()=>{
    console.log('express server running at http://127.0.0.1:8080');
})
